<!DOCTYPE html>
<html>

<head>
    <title>发布提问</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>

    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/jrender/jrender.min.js"></script>


    <script>


        $(function () {


                //发请求查询所有的地区
                $.get('/regions', function (data) {
                    //清空
                    $('.regionList').empty();
                    //所有地区的回显,循环遍历
                    $.each($(data), function (index, option) {
                        var child = "<option value=" + option.id + ">" + option.name + "</option>";
                        $('.regionList').append(child)
                    });

                });

                /*//eleditor 插件
            var Edr = new Eleditor({
                el: '#contentEditor',
                upload:{
                    server: '/images'
                }
            });*/

                //上传封面
                $('#changeBtn').click(function () {
                    //触发文件上传框
                    $('#btn-file').click();
                });
                //当文件上传框的值改变就发请求更新
                $('#fileForm').change(function () {
                    //判断是否有上传文件
                    if ($('#btn-file').val()) {
                        //提交表单
                        $('#fileForm').ajaxSubmit(function (data) {
                            console.log(data);
                            //根据返回结果判断上传结果
                            if (data.status == 1) {
                                $('#coverUrl').attr('src', data.url);
                            } else {
                                $(document).dialog({
                                    content: data.msg
                                })
                            }
                        })
                    }
                });


                //修改表单提交方式并提交
                $('#detailForm').ajaxForm({
                    //提交前的执行函数
                    beforeSubmit: function (arr) {
                        //提交表单时传递的数据
                        arr.push({name: "title", value: $('#title').val()});
                        arr.push({name: "coverUrl", value: $('#coverUrl').attr('src')});
                        //arr.push({name:"content",value:Edr.getContent()});
                    },
                    success: function (data) {
                        if (data.success) {
                            window.parent.location.href = "/questionPage.html";
                        } else {
                            $(document).dialog({
                                content: data.msg
                            })
                        }
                    }
                });
                //点击发布保存
                $('#release').click(function () {
                    $('#detailForm').submit();
                })


        })


    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-7"><h5 style="color: white">旅途问答</h5></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group" >
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px"  />

            <form id="fileForm" method="post" action="/images" enctype="multipart/form-data">
                <input type="file" id="btn-file" style="display: none" name="file">
            </form>
            <button type="button" class="btn" id="changeBtn">

                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
       

    </div>
    <div class="title">
        <input class="form-control" id="title"  placeholder="请输入标题"  />
    </div>
   <!-- <h6 href="#">点击即可编辑</h6>
    <div id="contentEditor"  >
    </div>-->
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/questions/save">
            <label>问题明细:</label><br>
            <textarea class="form-control regionList" placeholder="请输入问题描述" name="content"></textarea>
            <br>
            <label>地点:</label>
            <select class="form-control regionList" name="region.id" render-value="place.id" >
            </select>
        </form>
    </div>

    <hr/>

</div>


</body>

</html>